

cc.Class({
    extends: cc.Component,

    properties: {
        coinPre: [cc.Prefab],
        groundNode: cc.Node,
    },



    onLoad() {
        this.height = 0;
        this.deltalHeight = 25;
        this.dir = 1;
        this.index = Math.floor(Math.random() * (this.coinPre.length - 0) + 0);
        this.createCoin(this.index);
        //this.setCreateCoinNumber();
        this.schedule(() => {
            this.dir *= -1;
            this.index = Math.floor(Math.random() * (this.coinPre.length - 0) + 0);
        }, 6)
        this.schedule(() => {
            this.createCoin(this.index);
        }, 0.3);
    },


    start() {
    },
    update(dt) {
        // let maxHeight = cc.winSize.height / 2 - this.groundNode.height / 2;
        // let minHeight = -cc.winSize.height / 2 + this.groundNode.height / 2 * 3;
        // this.height = Math.random() * (maxHeight - minHeight) + minHeight;
    },
    setCreateCoinNumber() {
        let maxNumber = 11;
        let minNumber = 4;
        let coinNumber = Math.floor(Math.random() * (maxNumber - minNumber) + minNumber);
        let index = Math.floor(Math.random() * (this.coinPre.length - 0) + 0);
        for (this.i = 0; this.i < coinNumber; this.i++) {
            this.createCoin(index);
        }
    },
    //产生金币
    createCoin(index) {
        this.height += this.dir * this.deltalHeight;
        if (this.height >= cc.winSize.height / 2 - this.groundNode.height) {
            this.height = cc.winSize.height / 2 - this.groundNode.height;
        } else if (this.height <= 0) {
            this.height = 0;
        }
        this.coinNode = cc.instantiate(this.coinPre[index]);
        this.coinNode.parent = this.node;
        this.coinNode.y = this.height;
        this.coinNode.x = cc.winSize.width / 2;
    }
});
